import { defineStore, createPinia } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import { LangStore as langInter } from "../interface/lang";
import { trans, transt } from "~/utils/lang";

export const LangStore = defineStore({
    id:"lang",
    state:():langInter=>({
        lang:"zh-CN",
        reqHeader:{}
    }),
    getters:{
        getLang:state=>state.lang,
        getReqHeader: (state) => state.reqHeader,
    },
    actions:{
        setLang(lang:lang){
            if(!lang )return ;

            this.lang =  lang
            
        },
        changeLang(lang:lang){
            
        },
        transl(obj:any):any{
            const type = this.lang ? this.lang : (navigator.language !== "zh-CN" ? 'zh-HK':'zh-CN');
            return trans(obj,type);
        },
        transt(obj:any,defaultLang:lang = 'zh-CN'):any{
            return transt(obj, defaultLang)
        },
        setReqHeader(reqHeader:{[key:string]:string}){
            this.reqHeader = reqHeader;
        },
    }
})

// piniaPersist(持久化)
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

export default pinia
